<?php

namespace app\web\controller;
use app\web\model\LineTableModel;
use app\web\model\LineTableTypeModel;
use app\common\controller\StoreBaseController;
use think\Db;
/**
 * 房台管理
 */
class LineTableController extends StoreBaseController
{
 	protected $model;
	protected $tabletypeModel;
	
    protected function _initialize() {
        parent::_initialize();
		$this->model = new LineTableModel();
		$this->tabletypeModel = new LineTableTypeModel();
    }
    /**
     * 房台管理
     * @author [lx] 
     */
    public function index( $page = 1){
    	$field = 't.*,g.type_name';
    	if(request()->isPost()){
		 	$param = input('post.');
			//房台名称
			if($param['table_name']){
				$where['t.table_name'] = ['like', '%'.$param['table_name'].'%'];
			}	 				
	 	}
    	$where['t.store_id']= session('storeInfo')['id'];
		$list=Db::name("line_table")->alias('t')
		->join('line_tabletype g','g.id=t.table_typeid','LEFT')
		->field($field)->where($where)->order("t.sort desc")->paginate(10);
        return $this->fetch('index', ['list' => $list]);
    }


    /**
     * 
     * 添加
     * @author [lx] 
     */
    public function add()
    {
    	//获取房台分类
		$typelist = $this->tabletypeModel->where('store_id',session('storeInfo')['id'])->select();		
        if(request()->isAjax()){
        	$param=input('post.');
			//分类所属店铺id
			$param['store_id']=session('storeInfo')['id'];			
			if ($this->model->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('line_table/index'));
            } else {
                $this->error('添加失败');
            }
        }
		$list=$this->model->select();
		$this->assign('typelist',$typelist);
        return $this->fetch();
    }


    /**
     * [edit 编辑]
     * @return [type] [description]
     * @author [lx]
     */
    public function edit($id)
    {
    	//获取房台分类
		$typelist = $this->tabletypeModel->where('store_id',session('storeInfo')['id'])->select();		
        if(request()->isAjax()){
            $param = input('post.');         
         	if ($this->model->allowField(true)->save($param,$id) !== false) {
                    $this->success('修改成功', url('line_table/index'));
            } else {
                $this->error('修改失败');
            }
        }
        $id = input('param.id');
        $this->assign('model',$this->model->find($id));
		$this->assign('typelist',$typelist);
        return $this->fetch();
    }



    /**
     * [del删除]
     * @author [lx] 
     */
    public function delete()
    {
        $id = input('param.id');
        if ($this->model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
	
	 /**
     * [tbaletypedel查询房台分类]
     * @author [lx] 
     */
    public function tabletype( $page = 1){
		$where['store_id']= session('storeInfo')['id'];
		$list = db("line_tabletype")->where($where)->select();
		return $this->fetch('tabletype', ['list' => $list]);
	}
	
	/**
	 * 新增分类
	 */
	public function addtype(){
		$data['store_id']=session('storeInfo')['id'];
		$data['type_name']=input("name");
		$res=db('line_tabletype')->insert($data);
		if($res){
			return json(['code'=>200,'msg'=>"保存成功"]);
		}else{
			return json(['code'=>408,'msg'=>"保存失败"]);
		}
	}
	
	 /**
     * 修改分类
     */
    public function edittype()
    {
    	$data['store_id']=session('storeInfo')['id'];
		$data['type_name']=input('name');
		$data['id']=input('id');
		$res=db('line_tabletype')->update($data);
		if($res){
			return json(['code'=>200,'msg'=>"修改成功"]);
		}else{
			return json(['code'=>408,'msg'=>"修改失败"]);
		}
          	
    }
	
	/**
     * [del删除]
     * @author [lx] 
     */
    public function deletetype()
    {
        $id = input('param.id');
        if ($this->tabletypeModel->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}